/**
 * @file
 *           ComandsImpl.cpp
 * @brief
 *          <fillin>purpose
 *
 * @par Author (last changes):
 *          - Herea Adrian
 *
 * @par Description:
 *
 *
 * @par Module-History:
 * @verbatim
 *  Date        Author                          Reason
 *  22.01.2010  Herea Adrian         Initial creation
 *  23.01.2010  Herea Adrian         Change dir implementation finished
  * @endverbatim
 *
 * @par Copyright Notice:
 * @verbatim
 * Copyright (C) 2010
 * All Rights Reserved.
 * The reproduction, transmission or use of this document or its contents is
 * not permitted without express written authority.
 * Offenders will be liable for damages. All rights, including rights created
 * by patent grant or registration of a utility model or design, are reserved.
 **/
#include "ComandsImpl.h"

#define SERVER_LIST "d:\\server_list.txt"
#define MAX_LINE_SIZE 255

ComandsImpl::ComandsImpl(void)
{
}

ComandsImpl::~ComandsImpl(void)
{
}

void
ComandsImpl::sendServersAddress(SocketServer* m_sockServer)
{
  if (m_sockServer != NULL)
    {
      Command *cmd = new Command();
      cmd->setId(CMD_GET_SERVERS_ADRESSES);
      cmd->clearParams();
      //TO DO: read the value from the config file
     // Param Serv1("Server", "89.41.199.72 5600");
      //Param Serv2("Server", "192.168.2.102 5100");
     // cmd->addParam(Serv1);
//      cmd->addParam(Serv2);

	  ServerList_t serverList;
	  bool retVal = getServerListFromFile(SERVER_LIST, serverList);
	  ServerList_t::iterator it = serverList.begin();
	  ServerList_t::iterator endit = serverList.end();
	  while(it != endit)
	  {
		  Param param("Server", it->c_str());
		  cmd->addParam(param);
		  ++it;
	  }
      m_sockServer->protocolSend(Protocol::protocol(*cmd));
      delete cmd;
    }
}

bool  ComandsImpl::getServerListFromFile(char* filename, ServerList_t& outList)
{
	bool retVal = true;

	FILE* f = fopen(filename, "r");
	if( f != NULL )
	{
		char line[MAX_LINE_SIZE];
		while(fgets(line, MAX_LINE_SIZE, f) != NULL)
		{
			std::string szServer;
			szServer += line;
			outList.push_back(szServer);
		}
		fclose(f);
	}
	else
	{
		printf("ComandsImpl::getServerListFromFile : server list does not exist \n");
		retVal = false;
	}

	return retVal;
}

void ComandsImpl::sendChangeDirResult(SocketServer* m_sockServer,string path)
{
  Command *cmd = new Command();
  cmd->setId(CMD_CHANGE_DIR);
  cmd->clearParams();
  Param Ppath("path", path);
  Param Prasp("exist", "true");
  if (!m_FSOperations->checkPathExists(const_cast<char*> (path.c_str())))
    {
      Prasp.setValue("false");
    }
  cmd->addParam(Prasp);
  cmd->addParam(Ppath);
  m_sockServer->protocolSend(Protocol::protocol(*cmd));
  delete cmd;

}

void
ComandsImpl::sendDirContent(SocketServer* m_sockServer, string path)
{
  Command *cmd = new Command();

   LSResult_t content;
  if (m_FSOperations->listDirectory(const_cast<char*> (path.c_str()), content))
    {
      cmd->setId(CMD_LIST_DIR);
      cmd->clearParams();
	  LSResult_t::iterator it = content.begin();
	  for (it = content.begin();it!= content.end(); it ++)
	  {
		  Param type ("type","file");
		  if ((*it).second == IS_FOLDER)
		  {
			  type.setValue("folder");
		  }		  
		   Param name ("name",(*it).first);
		   cmd->addParam(name);
		   cmd->addParam(type);
	  }
	  //iterator 
	  //for (it)
      //Param rasp("filename", "file1");
      //se completeaza cufisierea din lista
//      cmd->addParam(rasp);
    }
  else
    {
      cmd->setId(CMD_LIST_END);
      //m_FSOperations->listDirectory(path,content);
    }
  m_sockServer->protocolSend(Protocol::protocol(*cmd));
  delete cmd;

}

void
ComandsImpl::sendError(SocketServer* m_sockServer, string Errorcode)
{
  Command *cmd = new Command();
  cmd->setId(CMD_LIST_END);
  cmd->clearParams();
  Param rasp("error", Errorcode);
  cmd->addParam(rasp);
  m_sockServer->protocolSend(Protocol::protocol(*cmd));
  delete cmd;

}

void ComandsImpl::sendAvailableSpace(SocketServer* m_sockServer)
{
	Command cmd;
	cmd.setId(CMD_QUERY_AVAILABLE_SPACE);
	cmd.clearParams();
	int freeSpace = (int)m_FSOperations->getFreeSpace();
	char szFreeSpace[20];
	_itoa(freeSpace, szFreeSpace, 10);

	Param rasp(AVAILABLE_SPACE_PARAM, szFreeSpace);
	cmd.addParam(rasp);
	m_sockServer->protocolSend(Protocol::protocol(cmd));
}

void ComandsImpl::sendCreateNewFile(SocketServer* m_sockServer, std::string path)
{
	Command cmd;
	cmd.setId(CMD_NEW_FILE);
	cmd.clearParams();

	Param rasp;
	if(!m_FSOperations->createFile(const_cast<char*>( path.c_str() ), IS_FILE))
	{
		rasp.setName(STATUS_PARAM);
		rasp.setValue("Could not create file");
	}
	else
	{
		rasp.setName(STATUS_PARAM);
		rasp.setValue(STATUS_OK_VALUE);
	}
	
	cmd.addParam(rasp);
	m_sockServer->protocolSend(Protocol::protocol(cmd));


}

void ComandsImpl::sendPathExists(SocketServer* m_sockServer, std::string path)
{
	Command cmd;
	cmd.setId(CMD_PATH_EXISTS);
	cmd.clearParams();
	bool bExists = m_FSOperations->checkPathExists(const_cast<char*>( path.c_str() ) );
	
	Param rasp(STATUS_PARAM, bExists == true ? STATUS_OK_VALUE : "File does not exist" );
	cmd.addParam(rasp);
	m_sockServer->protocolSend(Protocol::protocol(cmd));
}

void ComandsImpl::sendCreateNewDir(SocketServer* m_sockServer, std::string path)
{
	Command cmd;
	cmd.setId(CMD_MAKE_DIR);
	cmd.clearParams();

	Param rasp;
	if(!m_FSOperations->createFile(const_cast<char*>( path.c_str() ), IS_FOLDER))
	{
		rasp.setName(STATUS_PARAM);
		rasp.setValue("Could not create directory");
	}
	else
	{
		rasp.setName(STATUS_PARAM);
		rasp.setValue(STATUS_OK_VALUE);
	}
	
	cmd.addParam(rasp);
	m_sockServer->protocolSend(Protocol::protocol(cmd));

}